package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.core.util.QRCodeUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.basic.service.impl.BasicWarehouseServiceImpl;
import cn.stylefeng.guns.modular.hx.storage.entity.TurnoverContainer;
import cn.stylefeng.guns.modular.hx.storage.model.params.TurnoverContainerParam;
import cn.stylefeng.guns.modular.hx.storage.service.TurnoverContainerService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.mchange.v2.log.PackageNames;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * 周转容器管理控制器
 *
 * @author ZHDW
 * @Date 2024-04-10 09:58:05
 */
@Controller
@RequestMapping("/turnoverContainer")
public class TurnoverContainerController extends BaseController {

    private String PREFIX = "/hx/storage/turnoverContainer";

    @Autowired
    private TurnoverContainerService turnoverContainerService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @Autowired
    private BasicWarehouseService basicWarehouseService;
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        //查询可选择仓库
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/turnoverContainer.html";
    }

    /**
     * 新增页面
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/turnoverContainer_add.html";
    }

    /**
     * 编辑页面
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/turnoverContainer_edit.html";
    }

    /**
     * 新增接口
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TurnoverContainerParam turnoverContainerParam) {
        this.turnoverContainerService.add(turnoverContainerParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TurnoverContainerParam turnoverContainerParam) {
        this.turnoverContainerService.update(turnoverContainerParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TurnoverContainerParam turnoverContainerParam) {
        this.turnoverContainerService.delete(turnoverContainerParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TurnoverContainerParam turnoverContainerParam) {
        TurnoverContainer detail = this.turnoverContainerService.getById(turnoverContainerParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author ZHDW
     * @Date 2024-04-10
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TurnoverContainerParam turnoverContainerParam,String condition ,String startTime,String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            turnoverContainerParam.setStartTime("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            startTime = DateUtil.getDate();
            endTime = DateUtil.getDate();
            turnoverContainerParam.setStartTime("'"+startTime+"' and '"+endTime+"'");
        }
        User user = userService.getcurrentUser();
        turnoverContainerParam.setDeliverId(user.getDeliverId());
        turnoverContainerParam.setDeptId(user.getDeptId());
        turnoverContainerParam.setOperateId(user.getOperateId());
        return this.turnoverContainerService.findPageBySpec(turnoverContainerParam,condition);
    }

    /**
     * 去批量打印页面
     */
    @RequestMapping("/tobtnqrCode")
    public String tobtnqrCode() {
        return PREFIX + "/btnqrCode.html";
    }

    /**
     * 创建二维码
     */
    @RequestMapping(value = "/getqrcode", method = RequestMethod.GET)
    @ResponseBody
    public void generateQRCode4Product(String id, String name, HttpServletRequest request, HttpServletResponse response) {
        try {
            //logo
            File logoFile = new File("");
            String url = id;
            QRCodeUtil.drawLogoCode_128(logoFile, url, name,720,350,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();

        //查询可选择仓库
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/import.html";
    }

    /**
     * 下载模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcel")
    public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws ServletException , IOException {
        String path = "assets/excel/turnoverContainer.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("容器模板" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 导入excel
     */
    @RequestMapping(value = "/importExcel" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExceNewmethodl(@RequestPart("file") MultipartFile file, TurnoverContainerParam turnoverContainerParam)  {
        ResponseData data = new ResponseData();
        try {
            data = turnoverContainerService.getExceldata(file,turnoverContainerParam,data);//处理导入数据
        } catch (Exception e) {
            data.setSuccess(false);
            e.printStackTrace();
        }
        return data;
    }
}


